﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GC_School_of_Careers
{
    public partial class ucunit : UserControl
    {
        public ucunit()
        {
            InitializeComponent();
            this.tALUNITTableAdapter.Fill(this.dsunit.TALUNIT);
        }

        private void txtunitcode_TextChanged(object sender, EventArgs e)
        {
            DataView dv = new DataView();
            dv.Table = this.dsunit.Tables[0];
            dv.RowFilter = "CODE Like " + "'%" + txtunitcode.Text + "%'";
            dgsubject.DataSource = dv;
        }

        private void txtunitdescription_TextChanged(object sender, EventArgs e)
        {
            DataView dv = new DataView();
            dv.Table = this.dsunit.Tables[0];
            dv.RowFilter = "DESCR Like " + "'%" + txtunitdescription.Text + "%'";
            dgsubject.DataSource = dv;
        }

        private void txtunitsubjectcode_TextChanged(object sender, EventArgs e)
        {
            DataView dv = new DataView();
            dv.Table = this.dsunit.Tables[0];
            dv.RowFilter = "SUBJECTCODE Like " + "'%" + txtunitsubjectcode.Text + "%'";
            dgsubject.DataSource = dv;
        }

        private void txtunitsubjectdescription_TextChanged(object sender, EventArgs e)
        {
            DataView dv = new DataView();
            dv.Table = this.dsunit.Tables[0];
            dv.RowFilter = "SUBJECTDESCR Like " + "'%" + txtunitsubjectdescription.Text + "%'";
            dgsubject.DataSource = dv;
        }

        private void txtunitcentercode_TextChanged(object sender, EventArgs e)
        {
            DataView dv = new DataView();
            dv.Table = this.dsunit.Tables[0];
            dv.RowFilter = "CENTERCODE Like " + "'%" + txtunitcentercode.Text + "%'";
            dgsubject.DataSource = dv;
        }

        private void txtunitcentername_TextChanged(object sender, EventArgs e)
        {
            DataView dv = new DataView();
            dv.Table = this.dsunit.Tables[0];
            dv.RowFilter = "CENTERNAME Like " + "'%" + txtunitcentername.Text + "%'";
            dgsubject.DataSource = dv;
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            try
            {
                System.Data.OracleClient.OracleConnection conn = new System.Data.OracleClient.OracleConnection("Data Source=ATLANTIS;Persist Security Info=True;Password=GCS;User ID=GCS");
                conn.Open();
                string ORASQLCMD = "DELETE FROM TALUNIT";
                System.Data.OracleClient.OracleCommand command = new System.Data.OracleClient.OracleCommand(ORASQLCMD);
                command.Connection = conn;
                command.ExecuteNonQuery();

                for (int i = 0; i < (this.dgsubject.Rows.Count - 1); ++i)
                {
                    ORASQLCMD = "INSERT INTO TALUNIT VALUES ("
                    + "'" + dgsubject.Rows[i].Cells[0].Value.ToString() + "'"
                    + ",'" + dgsubject.Rows[i].Cells[1].Value.ToString() + "'"
                    + ",'" + dgsubject.Rows[i].Cells[2].Value.ToString() + "'"
                    + ",'" + dgsubject.Rows[i].Cells[3].Value.ToString() + "'"
                    + ",'" + dgsubject.Rows[i].Cells[4].Value.ToString() + "'"
                    + ",'" + dgsubject.Rows[i].Cells[5].Value.ToString() + "'"
                    + ",'" + dgsubject.Rows[i].Cells[6].Value.ToString() + "'"
                    + ",'" + dgsubject.Rows[i].Cells[7].Value.ToString() + "'"
                    + ",'" + dgsubject.Rows[i].Cells[8].Value.ToString() + "')";


                    System.Data.OracleClient.OracleCommand command1 = new System.Data.OracleClient.OracleCommand(ORASQLCMD);
                    command1.Connection = conn;
                    command1.ExecuteNonQuery();
                }
                conn.Close();
                this.tALUNITTableAdapter.Fill(this.dsunit.TALUNIT);
                MessageBox.Show("Update Succesful");
            }
            catch (Exception ex)
            {
                MessageBox.Show("Update failed");
            }
        }
    }
}
